<template>
  <div class="PayConfigPage page">
    <el-form inline :model="query" class="formSearch">
      <el-form-item prop="id" label="ID">
        <el-input v-model="query.id" placeholder="请输入"></el-input>
      </el-form-item>
      <el-form-item prop="configName" label="配置名称">
        <el-input v-model="query.configName" placeholder="请输入"></el-input>
      </el-form-item>
      <el-form-item prop="configStatus" label="配置状态">
        <el-select v-model="query.configStatus" placeholder="请选择">
          <el-option label="启用" value="1"></el-option>
          <el-option label="禁用" value="0"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item prop="configType" label="配置类型">
        <el-select v-model="query.configType" placeholder="请选择">
          <el-option label="微信" value="wx"></el-option>
          <el-option label="小程序" value="xcx"></el-option>
          <el-option label="支付宝" value="zfb"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label-width="0px">
        <el-button type="primary" @click="handleSearch">查询</el-button>
        <el-button @click="handleReset">重置</el-button>
        <el-button :icon="Plus" type="success" @click="handleAdd">新增配置</el-button>
      </el-form-item>
    </el-form>

    <div class="table-box">
      <el-table
        ref="tableRef"
        v-loading="state.tableLoading"
        :data="state.tableData"
        row-key="permissionId"
        border
        :header-cell-style="{ background: '#EEF3FF', color: '#333333' }"
        :style="{ width: '100%' }"
        class="dataTable"
      >
        <el-table-column prop="id" label="ID" width="60"></el-table-column>
        <el-table-column prop="configName" label="配置名称"></el-table-column>
        <el-table-column prop="configDesc" label="配置描述" show-overflow-tooltip></el-table-column>
        <el-table-column prop="configStatus" label="配置状态">
          <template #default="scope">
            <el-tag v-if="scope.row.configStatus == 1" type="success">启用</el-tag>
            <el-tag v-else type="danger">禁用</el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="configType" label="配置类型"></el-table-column>
        <el-table-column prop="createTime" label="创建时间"></el-table-column>
        <el-table-column prop="createByName" label="创建人"></el-table-column>
        <el-table-column prop="updateTime" label="更新时间"></el-table-column>
        <el-table-column prop="updateByName" label="更新人"></el-table-column>
        <el-table-column prop="handle" label="操作">
          <template #default="scope">
            <el-button type="primary" link @click="handleEdit(scope.row)">编辑</el-button>
            <el-button type="danger" link @click="handleDelete(scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <div class="fenye">
        <el-pagination
          v-model:current-page="query.page"
          :page-sizes="[10, 20, 50, 100]"
          :page-size="query.pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          background
          :total="query.total"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </div>
    </div>
    <DrawerEdit ref="drawerEditRef" @ok="getTableData" />
  </div>
</template>

<script setup>
import { postPayConfigGetAll } from '@/api/shop'
import { Plus } from '@element-plus/icons-vue'
import { onBeforeMount, reactive, ref } from 'vue'
import DrawerEdit from './drawerEdit.vue'
import { postPayConfigDelete } from '@/api/shop'
import { ElMessage, ElMessageBox } from 'element-plus'

const state = reactive({
  tableLoading: false,
  tableData: []
})

const types = {
  wx: '微信',
  xcx: '小程序',
  zfb: '支付宝'
}
const query = reactive({
  page: 1,
  pageSize: 10,
  total: 0,
  id: '',
  configName: '',
  configStatus: '', // 配置状态(0禁用，1启用)
  configType: '' // 配置类型(wx：微信，xcx:小程序，zfb：支付宝)
})

function getTableData() {
  state.tableLoading = true
  postPayConfigGetAll({ ...query })
    .then((res) => {
      state.tableData = res.data.list
      query.total = res.data.total
    })
    .finally(() => {
      state.tableLoading = false
    })
}

function handleSearch() {
  query.page = 1
  getTableData()
}

function handleReset() {
  query.page = 1
  query.pageSize = 10
  query.configType = ''
  query.configName = ''
  query.configStatus = null
  getTableData()
}

function handleCurrentChange(val) {
  state.page = val
  getTableData()
}
function handleSizeChange(val) {
  state.pageSize = val
  getTableData()
}

const drawerEditRef = ref(null)
function handleEdit(row) {
  drawerEditRef.value.show(row)
}

function handleAdd() {
  drawerEditRef.value.show()
}

function handleDelete(row) {
  ElMessageBox.confirm('此操作将删除该数据, 是否继续?', '提示', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning'
  }).then(() => {
    postPayConfigDelete({ id: row.id, shopId: row.shopId }).then((res) => {
      ElMessage.success('删除成功')
      getTableData()
    })
  })
}

onBeforeMount(() => {
  getTableData()
})
</script>
